import { ready as cryptoReady, tcrypto } from '@tanker/crypto';
import { expect } from '@tanker/test-utils';

import {
  serializeProvisionalIdentityClaim,
  unserializeProvisionalIdentityClaim,
} from '../Serialize';

import { makeUint8Array } from '../../__tests__/makeUint8Array';

// NOTE: If you ever have to change something here, change it in the Go code too!
// The test vectors should stay the same
describe('provisional identity serialization: payload test vectors', () => {
  it('correctly deserializes a ProvisionalIdentityClaim test vector', async () => {
    await cryptoReady;
    const provisionalIdentityClaim = {
      user_id: makeUint8Array('the user id', tcrypto.HASH_SIZE),
      app_provisional_identity_signature_public_key: makeUint8Array('the app sig pub key', tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
      tanker_provisional_identity_signature_public_key: makeUint8Array('the tanker sig pub key', tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
      author_signature_by_app_key: makeUint8Array('the author sig by app key', tcrypto.SIGNATURE_SIZE),
      author_signature_by_tanker_key: makeUint8Array('the author sig by tanker key', tcrypto.SIGNATURE_SIZE),
      recipient_user_public_key: makeUint8Array('user pub key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
      encrypted_provisional_identity_private_keys: makeUint8Array('both encrypted private keys', tcrypto.ENCRYPTION_PRIVATE_KEY_SIZE * 2
                                                                                      + tcrypto.SEAL_OVERHEAD),
    };

    const payload = new Uint8Array([
      // UserId
      0x74, 0x68, 0x65, 0x20, 0x75, 0x73, 0x65, 0x72, 0x20, 0x69, 0x64, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      // AppProvisionalIdentitySignaturePublicKey
      0x74, 0x68, 0x65, 0x20,
      0x61, 0x70, 0x70, 0x20, 0x73, 0x69, 0x67, 0x20, 0x70, 0x75, 0x62, 0x20,
      0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00,
      // TankerProvisionalIdentitySignaturePublicKey
      0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x6e, 0x6b,
      0x65, 0x72, 0x20, 0x73, 0x69, 0x67, 0x20, 0x70, 0x75, 0x62, 0x20, 0x6b,
      0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      // AuthorSignatureByAppKey
      0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x20, 0x73,
      0x69, 0x67, 0x20, 0x62, 0x79, 0x20, 0x61, 0x70, 0x70, 0x20, 0x6b, 0x65,
      0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00,
      // AuthorSignatureByTankerKey
      0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68,
      0x6f, 0x72, 0x20, 0x73, 0x69, 0x67, 0x20, 0x62, 0x79, 0x20, 0x74, 0x61,
      0x6e, 0x6b, 0x65, 0x72, 0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      // RecipientUserPublicKey
      0x75, 0x73, 0x65, 0x72, 0x20, 0x70, 0x75, 0x62, 0x20, 0x6b, 0x65, 0x79,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      // EncryptedProvisionalIdentityPrivateKeys
      0x62, 0x6f, 0x74, 0x68,
      0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x20, 0x70,
      0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x73, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ]);

    expect(serializeProvisionalIdentityClaim(provisionalIdentityClaim)).to.deep.equal(payload);
    expect(unserializeProvisionalIdentityClaim(payload)).to.deep.equal(provisionalIdentityClaim);
  });
});
